//
//  CollectedViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-2-6.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "CollectedViewController.h"
#import "CollectedCell.h"
#import "BookViewController.h"

@interface CollectedViewController ()
@property (nonatomic,retain) NSArray *collectedArray;
@end

@implementation CollectedViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotCollected:) name:gotCollectedNotify object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotCollected:) name:gotCommentedNotify object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotCollected:) name:gotSharedNotify object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    collectedGridView.requiresSelection = NO;
    collectedGridView.backgroundViewExtendsUp = YES;
    collectedGridView.backgroundViewExtendsDown = YES;
    [self updateDisplay];
}

- (void)updateDisplay
{
    switch (self.actionType) {
        case UserCollect:
            [[DataRequest sharedRequest] getCollected];
            break;
        case UserComment:
            [[DataRequest sharedRequest] getCommented];
            break;
        case UserShare:
            [[DataRequest sharedRequest] getShared];
            break;
        default:
            break;
    }
}

- (void)gotCollected:(NSNotification *)notification
{
    if ([notification.object isKindOfClass:[NSDictionary class]]) {
        NSArray *array = [notification.object valueForKey:@"result"];
        if ([array isKindOfClass:[NSArray class]]) {
            self.collectedArray = array;
            [collectedGridView reloadData];
        }
    }
}

- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
{
    return self.collectedArray.count;
}

- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
{
    CollectedCell *cell = (CollectedCell *)[collectedGridView dequeueReusableCellWithIdentifier:@"CollectedCell"];
    if (cell == nil) {
        cell = [[CollectedCell alloc] initWithFrame:CGRectMake(0, 0, 96, 128) reuseIdentifier:@"CollectedCell"];
        cell.selectionStyle = AQGridViewCellSelectionStyleBlue;
    }
    cell.infoDic = [self.collectedArray objectAtIndex:index];
    [cell updateDisplay];
    return cell;
}

- (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
{
    BookViewController *bookViewController = [[[BookViewController alloc] initWithNibName:@"BookViewController" bundle:nil] autorelease];
    bookViewController.itemid = [[self.collectedArray objectAtIndex:index] valueForKey:@"item_id"];
    bookViewController.itemname = [[self.collectedArray objectAtIndex:index] valueForKey:@"item_name"];
    
    UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
    [navController pushViewController:bookViewController animated:YES];
    [collectedGridView deselectItemAtIndex:index animated:NO];
    
    [gridView deselectItemAtIndex:index animated:NO];
}

- (IBAction)navBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [collectedGridView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [collectedGridView release];
    collectedGridView = nil;
    [super viewDidUnload];
}
@end
